/*
paginateTable 1.0
written by Matthew Page
licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
*/
(function ($) {
  $.fn.paginateTable = function(options) {
  
      var settings = jQuery.extend({
          rowsPerPage: 5,               /* the number of rows that comprise a page */
          nextPage: ".nextPage", 		/* selector for "Next Page" dom element. Click to change page. */
          prevPage: ".prevPage",		/* selector for "Previous Page" dom element. Click to change page. */ 
          currentPage: ".currentPage",	/* selector for "Current Page" dom element. Display only. */
          totalPages: ".totalPages",	/* selector for "Total Pages" dom element. Display only. */
          pager: ".pager",		        /* selector for container of all paging dom elements */
          listPages: '.listPages',
          page: '.pagesNumber',
          autoHidePager: true		    /* hides the pager (see selector above) if there is only a single page */
      }, options || {});
  
      return this.each(function(){
          var table = $(this);
          var rows = table.find("tbody tr");
          var totalPages = Math.ceil(rows.size() / settings.rowsPerPage);
          var i;
    	  var pager = "";
          for (i = 1; i <= totalPages; i++)
    	  {
        	  pager += '<li><a href="#" class="pagesNumber button-gray" id="' + i + '">' + i + '</a></li>';
    	  }
          var nextPage = $(settings.nextPage);
          var prevPage = $(settings.prevPage);
          var currentPage = $(settings.currentPage);
          var page = $(settings.page);
          $(settings.listPages).append(pager);
          nextPage.click(function(){
                  var pageNum = getCurrentPage(currentPage.text());
                  displayPage(table, pageNum+1, settings);
                  return false;
              });
          
          prevPage.click(function(){
                  var pageNum = getCurrentPage(currentPage.text());
                  displayPage(table, pageNum-1, settings);
                  return false;
              });
          displayPage(table, getCurrentPage(currentPage.text()), settings);
          $(".pagesNumber").each(function(){
        	  $(this).click(function(){
        		  $(".pagesNumber").removeClass('current');
        		  displayPage(table, $(this).attr('id'), settings);
        		  $(this).addClass('current');
        		  return false;
        	  });
          });
      });
  };
  
  function getCurrentPage(pageText){
      var pageNum = parseInt(pageText,10);
      if (isNaN(pageNum)){
          pageNum = 0;
      }
      return Math.max(1, pageNum);
  }
  
  function displayPage(table, pageNum, settings){
      pageNum = Math.max(1, pageNum);
      if (settings.rowsPerPage > 0){
          var rows = table.find("tbody tr");
          var totalPages = Math.ceil(rows.size() / settings.rowsPerPage);
          if (settings.autoHidePager && totalPages <= 1){
              $(settings.pager).hide();
          }
          else if (totalPages > 0){
        	  
              pageNum = Math.min(pageNum, totalPages);
              var rowStartIndex = (pageNum - 1) * settings.rowsPerPage;
              var rowEndIndex = pageNum * settings.rowsPerPage;
              $.each(rows, function(index, row){
                  if (index >= rowStartIndex && index < rowEndIndex){
                      $(row).show();
                  }
                  else{
                      $(row).hide();
                  }
              });
              $(settings.currentPage).text(pageNum);
              $(settings.totalPages).text(totalPages);
              $(".pagesNumber").removeClass('current');
              $("#"+pageNum).addClass('current');
          }
          
      }
  }
	
})(jQuery);

$.fn.sortElements = (function(){

    var sort = [].sort;

    return function(comparator, getSortable) {

        getSortable = getSortable || function(){return this;};

        var placements = this.map(function(){

            var sortElement = getSortable.call(this),
                parentNode = sortElement.parentNode,

            // Since the element itself will change position, we have
            // to have some way of storing its original position in
            // the DOM. The easiest way is to have a 'flag' node:
            nextSibling = parentNode.insertBefore(
                document.createTextNode(''),
                sortElement.nextSibling
            );

            return function() {

                if (parentNode === this) {
                    throw new Error(
                        "You can't sort elements if any one is a descendant of another."
                    );
                }

                // Insert before flag:
                parentNode.insertBefore(this, nextSibling);
                // Remove flag:
                parentNode.removeChild(nextSibling);

            };

        });

        return sort.call(this, comparator).each(function(i){
            placements[i].call(getSortable.call(this));
        });

    };

})();

$.fn.tablesort = (function(options) {
    return this.each(function() {  
        var table = $(this);
        $(this).find('thead th').wrapInner('<a href="#"/>').find('a').click(function(){
          var sort = $(this).data('sort');
          $(this).parents('thead').find('a').removeClass('sort-asc sort-desc');
          sort = (sort=='asc'? 'desc' : (sort=='desc'? 'asc' : 'asc'));
          $(this).data('sort', sort).addClass('sort-'+sort);
          table.find('tbody tr td').removeClass('column-selected');
          table.find('tbody tr td:nth-child('+($(this).parent().index()+1)+')').sortElements(
            function(a, b){
                return sort=='desc'? ($(a).text() < $(b).text()) - ($(a).text() > $(b).text()) : ($(a).text() > $(b).text()) - ($(a).text() < $(b).text());
            },
            function(){
                return this.parentNode; 
            }
          ).addClass('column-selected');
          return false;
        });
        return $(this);
    });
});

